Ext.onReady(function() {
			var bar = new Ext.Toolbar([{
						text : "增加学生",
						icon : '../../resource/add.gif',
						handler : addStudent
					}, {
						text : "删除学生资料",
						icon : '../../resource/delete.png',
						handler : function() {
							Ext.MessageBox.buttonText.yes = '确定';
							Ext.MessageBox.buttonText.no = '取消';
							var selectModel = grid.getSelectionModel();
							var selectRecord = selectModel.getSelections();
							if (selectRecord.length == 0) {
								Ext.Msg.alert('', '请选择至少一个学生');
								return;
							}
							Ext.Msg.show({
										msg : '确定删除所选的学生吗？删除后将无法恢复',
										buttons : Ext.Msg.YESNO,
										fn : function(bt) {
											if (bt == 'yes') {
												deleteStudent(selectRecord);
											};
										},
										icon : Ext.MessageBox.QUESTION
									});
						}
					}

			]);
	var cb=new Ext.grid.CheckboxSelectionModel();
	var store = new Ext.data.JsonStore({
        root: 'data',
        totalProperty: 'totalcount',
        fields: [
        	'dbID','userID','name','sex','birthday','qq','email','tel','addr','grade'
        ],
        proxy: new Ext.data.HttpProxy({
            url: 'GetStudentAction'
        }),
        autoLoad:{params:{start:0,limit:50}}
    });
    var edit=new Ext.form.TextField({
               allowBlank: false
    });
	var grid=new Ext.grid.EditorGridPanel({
		columnLines: true,
		region:'center',
		loadMask:true,
		listeners:
		{
			afteredit:updateStudent
		},
		tbar:[
			bar,
			new Ext.PagingToolbar({
				store:store,
				pageSize:50,
				displayInfo: true,
				displayMsg:'第 {0} 条到 {1} 条,一共 {2} 条',
				emptyMsg:'没有记录'
			})
		],
		stripeRows:true,
		autoScroll:true,
		applyTo:'grid_div',
		viewConfig:{autoFill:true},
		frame:true,
		sm:cb,
		store:store,
		columns:[
		new Ext.grid.RowNumberer({
			header:'行号',
			width:40
		}),
		cb,
		{header:"数据库编号",width:20,dataIndex:'dbID',align:'center',sortable:true},
		{header:"学号",width:20,dataIndex:'userID',align:'center',editor:edit,sortable:true},
		{header:"姓名",width:30,dataIndex:'name',align:'center',editor:edit,sortable:true},
		{header:"性别",width:15,dataIndex:'sex',align:'center',editor:new Ext.form.ComboBox({
			editable:false,
			displayField:'sex',
			mode:'local',
			triggerAction:'all',
			store:new Ext.data.SimpleStore({
				fields:['sex'],
				data:[['男'],['女']]
			})
		}),sortable:true},
		{header:"生日",width:25,dataIndex:'birthday',renderer:Ext.util.Format.dateRenderer('Y-m-d'),
			editor:new Ext.form.DateField({
			format: 'y-m-d'
		}),align:'center'},
		{header:"QQ",width:25,dataIndex:'qq',
			editor:new Ext.form.TextField({
				regex:/^[0-9]+$/,
				regexText:'只能输入数字',
				msgTarget : 'title'
			}),align:'center'},
		{header:"Email",width:35,dataIndex:'email',
			editor:new Ext.form.TextField({
				vtype:'email'
			}),align:'center'},
		{header:"联系电话",width:25,dataIndex:'tel',editor:edit,align:'center'},
		{header:"年级",width:30,dataIndex:'grade',editor:edit,align:'center'},
		{header:"家庭住址",width:40,dataIndex:'addr',editor:edit,align:'center'}
		]
	});
/*	var tip_label = new Ext.form.Label({
			id:'tip_label',
			xtype : 'label',
			html:'<img src="../../extjs/resources/images/default/grid/loading.gif" />请稍后...',
			hidden:true
		});*/
	var studentForm=new Ext.FormPanel({
		labelAlign:'right',
		frame:true,
		buttonAlign:'center',
		id:'addStudentForm',
		name:'addStudentForm',
		items:[
		{
			xtype:'textfield',
			width:150,
			allowBlank:false,
			name:'userID',
		//	id:'userID',
			fieldLabel:'学号'
		},
		{
			xtype:'textfield',
			width:150,
			name:'name',
		//	id:'name',
			fieldLabel:'姓名'
		},
		{
			xtype:'textfield',
			width:150,
			name:'grade',
		//	id:'grade',
			fieldLabel:'年级'
		},
		{
			xtype:'datefield',
			width:150,
			allowBlank:false,
			name:'birthday',
	//		id:'birthday',
			fieldLabel:'生日'
		},
		{
			xtype:'textfield',
			width:150,
			name:'qq',
		//	id:'qq',
			fieldLabel:'QQ'
		},
		{
			xtype:'textfield',
			width:150,
			name:'email',
	//		id:'email',
			fieldLabel:'邮箱'
		},
		{
			xtype:'textfield',
			width:150,
			name:'tel',
		//	id:'tel',
			fieldLabel:'联系电话'
		},
		{
			xtype:'textfield',
			width:250,
			name:'addr',
	//		id:'addr',
			fieldLabel:'家庭住址'
		}
		],
	buttons:[
		new Ext.Button({
			text:'提交',
			handler:submitStudent
		}),
		new Ext.Button({
			text:'重置',
			handler:function(){studentForm.form.reset();}
		})
	]
	});
	new Ext.Viewport({
		layout:'border',
		items:[
			grid
		]
	});
	var win=new Ext.Window({
		plain:true,
		width:450,
		height:310,
		closeAction:'hide',
		listeners:
		{
			'beforehide':function(){store.reload();}
		},
		bodyStyle:'padding:5px',
		modal:true,
		closable:true,
		animCollapse:true,
		resizable:false,
		items:[studentForm
		]
	});
	function addStudent()
	{
		studentForm.form.reset();
		studentForm.isAdd=true;
		win.setTitle("添加学生");
		win.show();
	}
	function $value(id)
	{
		return document.getElementById(id).value;
	};
	function submitStudent()
	{
		var data=getExtForm(studentForm);
		var json=Ext.util.JSON.encode(data);
		Ext.ajaxRequest(json,'addStudentAction');
	};
	function updateStudent(e)
	{
		var data={};
		data['id']=e.record.get("dbID");
		data['field']=e.grid.getColumnModel().getDataIndex(e.column);
		data['value']=e.value;
		var json=Ext.util.JSON.encode(data);
		Ext.ajaxRequest(json,'updateStudentAction','更新成功','更新失败');

	}
	function deleteStudent(record)
	{
		
		var idArray=new Array();
		for(var i=0;i<record.length;++i)
		{
			var r=record[i];
			idArray[i]=r.get('dbID');
		}
		var json=Ext.util.JSON.encode(idArray);
		Ext.ajaxRequest(json,'deleteStudentAction');
		store.reload();
	}
});